<template>
  <van-row class="page-score-item-list">
    <van-nav-bar
      left-text="返回"
      right-text="确定"
      title="选择考核事项"
      @click-left="$emit('onCancel')"
      @click-right="doConfirm"/>
    <van-row class="page-score-item-list-header">
      <span>{{carrier.content_item.id + ' - ' +carrier.content_item.item.substr(0, carrier.content_item.item.indexOf('。'))}}</span>
    </van-row>
    <van-row class="page-score-item-list-content">
      <span>{{carrier.content_item.item.substr(carrier.content_item.item.indexOf('。') + 1)}}</span>
    </van-row>
    <van-divider dashed>考核事项</van-divider>
    <van-checkbox-group v-model="scoreItems">
      <van-cell-group>
        <card-score-item
          v-for="(score) in scoreList"
          :key="score.id"
          :score="score"/>
      </van-cell-group>
    </van-checkbox-group>
  </van-row>
</template>

<script>
import CardScoreItem from '@/views/common/card/CardScoreItem'
import { getCarrierDetail } from '@/api/assess'
export default {
  name: 'PageSoreItemList',
  components: { CardScoreItem },
  props: ['carrier', 'type'],
  data () {
    return {
      isLoading: false,
      scoreList: this.carrier.content_item.sub_items ? this.carrier.content_item.sub_items : [],
      scoreItems: []
    }
  },
  mounted () {
    if (!this.carrier.content_item.sub_items) {
      this.doGetCarrierDetail()
    }
  },
  methods: {
    doConfirm () {
      console.log('scoreItems', this.scoreItems)
      if (this.scoreItems.length === 0) {
        this.$dialog.alert({
          message: '请先选择考核事项'
        })
      } else {
        this.scoreItems.sort((obj1, obj2) => {
          var val1 = Number(obj1.id.split('.')[2])
          var val2 = Number(obj2.id.split('.')[2])
          if (val1 < val2) {
            return -1
          } else {
            return 1
          }
        })
        this.$emit('onConfirm', this.scoreItems)
      }
    },
    doGetCarrierDetail () {
      this.isLoading = true
      getCarrierDetail(this.carrier.id, this.type).then(res => {
        this.isLoading = false
        let { data } = res
        if (data[this.$response.CODE] === this.$response.SUCCESS) {
          let contentList = data[this.$response.DATA].check_detail
          let content = contentList.find((item) => {
            return item.id === this.carrier.content_item.id
          })
          content.sub_items.sort((obj1, obj2) => {
            var val1 = Number(obj1.id.split('.')[2])
            var val2 = Number(obj2.id.split('.')[2])
            if (val1 < val2) {
              return -1
            } else {
              return 1
            }
          })
          this.scoreList = content.sub_items
        } else {
          this.$toast.fail('获取数据失败：' + data[this.$response.MESSAGE])
        }
      }, errFunc => {
        this.isLoading = false
        this.$notify('服务调用失败：' + errFunc.toString())
        console.log(errFunc.toString())
      })
    }
  }
}
</script>

<style scoped lang="less">
  .page-score-item-list {
    .page-score-item-list-header {
      background-color: white;
      line-height: 40px;
      text-align: center;
      font-size: 14px;
      color: #1989FA;
    }
    .page-score-item-list-content {
      background-color: white;
      padding: 0px 16px 10px 16px;
      font-size: 12px;
      color: #969799;
    }
  }
</style>
